package org.common.utils.generatorentity.event.handler;

import org.common.utils.database.access.DataAccess;
import org.common.utils.database.access.DataAccessTool;
import org.common.utils.database.access.DataSourceTool;
import org.common.utils.event.annotation.EventMethod;
import org.common.utils.field.FieldInfo;
import org.common.utils.field.GeneratorConfig;
import org.common.utils.generatorentity.event.entity.GeneratorEvent;
import org.common.utils.generatorentity.event.handler.service.GeneratorService;
import java.io.File;
import java.util.List;

/**
 * @author zhouzhibing
 * @date 2025/8/5
 */
public class CommonEventHandler {

    @EventMethod
    public void doGenerator(GeneratorEvent event) {
        String tableName = event.getTableName();
        DataAccessTool.get().init("10.30.126.161:3306/crush_game_2" , "root" , "root");
        DataAccess dataAccess = DataAccessTool.get().getDataAccess();
        List<FieldInfo> list = dataAccess.select("show full columns from " + tableName, resultSet -> new FieldInfo(
                resultSet.getString(1),
                resultSet.getString(2),
                resultSet.getString(9)
        ));
        GeneratorConfig config = GeneratorConfig.newBuilder()
                        .setTableName(tableName)
                        .setFieldList(list)
                        .setPackageName("org.common.utils.generatorentity.entity")
                        .setWritePath("D:\\Workspaces\\idea-workspace\\common-utils\\generator-entity\\src\\main\\java").build();

        checkAndCreatePath(config);

        GeneratorService.getInstance().generator(config);
    }


    private void checkAndCreatePath(GeneratorConfig config) {
        String allPath = config.getAllPath();

        File generatorDirFile = new File(allPath);
        if(!generatorDirFile.exists()) {
            generatorDirFile.mkdirs();
        }
    }

}
